package cn.zebra.dev.mall.module.product.controller;

import cn.zebra.dev.common.restful.Result;
import cn.zebra.dev.common.utils.ObjectUtils;
import cn.zebra.dev.ibatis.plus.utils.PageUtils;


import cn.zebra.dev.mall.module.product.dto.*;
import cn.zebra.dev.mall.module.product.vo.ProductBrandSearchVO;
import cn.zebra.dev.mall.module.product.vo.ProductCategorySearchVO;
import cn.zebra.dev.mall.module.product.vo.ProductGroupSearchVO;
import cn.zebra.dev.mall.module.product.vo.ProductSearchVO;

import cn.zebra.dev.product.module.category.entity.CategoryEntity;
import cn.zebra.dev.product.module.category.service.CategoryService;
import cn.zebra.dev.product.module.product.entity.ProductEntity;
import cn.zebra.dev.product.module.product.service.ProductService;
import cn.zebra.dev.product.moudle.brand.entity.BrandEntity;
import cn.zebra.dev.product.moudle.brand.service.BrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "商品服务")
@RestController
@RequestMapping("/product")
public class ProductIndexController {

    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private BrandService brandService;


    @GetMapping("/indexList")
    @ApiOperation("商城首页")
    public Result<PageUtils<ProductIndexDTO>> indexList(
            @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page) {
        Map<String, Object> params = new HashMap<>();
        params.put("limit", limit);
        params.put("page", page);
        PageUtils<ProductEntity> pageUtils = productService.findPage(params);
        PageUtils<ProductIndexDTO> data = new PageUtils<>(pageUtils);
        List<ProductEntity> list = pageUtils.getList();
        if (list.size() > 0) {
            List<ProductIndexDTO> dtos = new ArrayList<>();
            list.forEach(entity -> {
                ProductIndexDTO dto = new ProductIndexDTO();
                BeanUtils.copyProperties(entity, dto);
                dtos.add(dto);
            });
            data.setList(dtos);
        }
        return Result.success(data);
    }


    @GetMapping("/search")
    @ApiOperation("商品搜索")
    public Result<PageUtils<ProductSearchDTO>> search(
            ProductSearchVO vo,
            @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page) {
        Map<String, Object> params = ObjectUtils.objectConvertMap(vo);
        params.put("limit", limit);
        params.put("page", page);
        params.put("productName",vo.getProductName());
        params.put("key",vo.getKey());
        params.put("sort",vo.getSort());
        params.put("minPrice",vo.getMinPrice());
        params.put("maxPrice",vo.getMaxPrice());
        PageUtils<ProductEntity> pageUtils = productService.findPageByProductName(params);
        PageUtils<ProductSearchDTO> data = new PageUtils<>(pageUtils);
        List<ProductEntity> list = pageUtils.getList();
        if (list.size() > 0) {
            List<ProductSearchDTO> dtos = new ArrayList<>();
            list.forEach(entity -> {
                ProductSearchDTO dto = new ProductSearchDTO();
                BeanUtils.copyProperties(entity, dto);
                dtos.add(dto);
            });
            data.setList(dtos);
            return Result.success(data);
        }
        return Result.success(data);
    }

    @GetMapping("/categorySearch")
    @ApiOperation("分类搜索")
    public Result<PageUtils<ProductSearchDTO>> categorySearch(
            ProductCategorySearchVO vo,
            @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page) {
        Map<String, Object> params = ObjectUtils.objectConvertMap(vo);
        params.put("limit", limit);
        params.put("page", page);
        params.put("id",vo.getId());
        params.put("productName",vo.getProductName());
        params.put("key",vo.getKey());
        params.put("sort",vo.getSort());
        params.put("minPrice",vo.getMinPrice());
        params.put("maxPrice",vo.getMaxPrice());
        PageUtils<ProductEntity> productEntities = productService.findPageByCategoryIdAndProductName(params);
        PageUtils<ProductSearchDTO> data = new PageUtils<>(productEntities);
        List<ProductEntity> list = productEntities.getList();
        List<ProductSearchDTO> dtos = new ArrayList<>();
        if (list.size() > 0) {
            list.forEach(entity -> {
                ProductSearchDTO dto = new ProductSearchDTO();
                BeanUtils.copyProperties(entity, dto);
                dtos.add(dto);
            });
            data.setList(dtos);
            return Result.success(data);
            //如果productEntities = 0 就代表根据分类id和商品名称的查询不能同时满足  再根据分类id查
        }else{
            PageUtils<ProductEntity> productEntityList = productService.findByCategoryId(params);
            PageUtils<ProductSearchDTO> dtoData = new PageUtils<>(productEntityList);
            List<ProductEntity> enitiyList = productEntities.getList();
            List<ProductSearchDTO> dtoss = new ArrayList<>();
            if(enitiyList.size() > 0){
                enitiyList.forEach(entity ->{
                    ProductSearchDTO dto = new ProductSearchDTO();
                    BeanUtils.copyProperties(entity,dto);
                    dtoss.add(dto);
                });
                dtoData.setList(dtoss);
                return  Result.success(dtoData);
            }
        }
        return Result.success(data);
    }

    @GetMapping("/screen/{id}")
    @ApiOperation("筛选")
    public Result<List<ProductBrandDTO>> screen(@PathVariable Long id){
        //根据分类id 查询品牌
        List<BrandEntity> list = brandService.findBrandByCategory(id);
        List<ProductBrandDTO> dtos = new ArrayList<>();
        if(list.size() > 0){
            list.forEach(entity -> {
                ProductBrandDTO dto = new ProductBrandDTO();
                BeanUtils.copyProperties(entity,dto);
                dtos.add(dto);
            });
        }
        return Result.success(dtos);
    }


    @GetMapping("/brandSearch")
    @ApiOperation("品牌搜索")
    public Result<PageUtils<ProductSearchDTO>> brandSearch(
            ProductBrandSearchVO vo,
            @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page) {
        Map<String, Object> params = ObjectUtils.objectConvertMap(vo);
        params.put("limit", limit);
        params.put("page", page);
        params.put("id",vo.getId());
        params.put("key",vo.getKey());
        params.put("sort",vo.getSort());
        params.put("minPrice",vo.getMinPrice());
        params.put("maxPrice",vo.getMaxPrice());
        PageUtils<ProductEntity> productEntities = productService.findPageByBrandId(params);
        PageUtils<ProductSearchDTO> data = new PageUtils<>(productEntities);
        List<ProductEntity>  list = productEntities.getList();
        List<ProductSearchDTO> dtos = new ArrayList<>();
        if (list.size() > 0) {
            list.forEach(entity -> {
                ProductSearchDTO dto = new ProductSearchDTO();
                BeanUtils.copyProperties(entity, dto);
                dtos.add(dto);
            });
            data.setList(dtos);
            return Result.success(data);
        }
        return Result.success(data);
    }



    @GetMapping("/groupSearch")
    @ApiOperation("分组搜索")
    public Result<PageUtils<ProductSearchDTO>> groupSearch(
            ProductGroupSearchVO vo,
            @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page) {
        Map<String, Object> params = ObjectUtils.objectConvertMap(vo);
        params.put("limit", limit);
        params.put("page", page);
        params.put("id",vo.getId());
        params.put("productName",vo.getProductName());
        params.put("key",vo.getKey());
        params.put("sort",vo.getSort());
        params.put("minPrice",vo.getMinPrice());
        params.put("maxPrice",vo.getMaxPrice());
        PageUtils<ProductEntity> productEntities = productService.findPageByGroupIdAndProductName(params);
        PageUtils<ProductSearchDTO> data = new PageUtils<>(productEntities);
        List<ProductEntity> list = productEntities.getList();
        List<ProductSearchDTO> dtos = new ArrayList<>();
        if (list.size() > 0) {
            list.forEach(entity -> {
                ProductSearchDTO dto = new ProductSearchDTO();
                BeanUtils.copyProperties(entity, dto);
                dtos.add(dto);
            });
            data.setList(dtos);
            return Result.success(data);
        }
        return Result.success(data);
    }
}






